package com.helpyoutimely.order.adapter;/**
 * ${PROJET_NAME}
 * Created by lixinkun on 16/4/13 19:45.
 * Email EricLi1235@gmail.com.
 */

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.helpyoutimely.R;
import com.helpyoutimely.entity.ConsultExpert;

import java.util.List;

/**
 * User: lixinkun
 * Date: 2016-04-13
 * Time: 19:45
 * FIXME
 */
public class ExpertListAdapter extends BaseExpandableListAdapter{
    private List<ConsultExpert> mList;
    private Context mContext;

    public ExpertListAdapter(List<ConsultExpert> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return mList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return mList.get(i).getDescription();
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        View v = View.inflate(mContext, R.layout.head_expert,null);
        final ConsultExpert c = mList.get(i);
        ImageView imgCall = (ImageView) v.findViewById(R.id.btn_consult);
        ImageView imgExpert = (ImageView) v.findViewById(R.id.img_expert);
        imgExpert.setBackgroundResource(c.getImg());
        imgCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 createDialog(c.getPhone());
            }
        });
        TextView tvName = (TextView) v.findViewById(R.id.tv_name);
        TextView tvTags = (TextView) v.findViewById(R.id.tv_tags);
        TextView tvOrderNums = (TextView) v.findViewById(R.id.tv_ordernum);
        TextView tvPlace = (TextView) v.findViewById(R.id.tv_place);
        RatingBar ratingBar = (RatingBar) v.findViewById(R.id.ratingBar);
        ratingBar.setRating(c.getRate());
        tvTags.setText(c.getTags());
        tvName.setText(c.getName());
        tvOrderNums.setText(c.getOrdernums());
        tvPlace.setText(c.getPlace());
        return v;
    }

    private void createDialog(final String phone){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("系统提示");
        builder.setMessage("是否拨打电话,电话接通以后开始计时");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //                Toast.makeText(mContext,"拨打电话",Toast.LENGTH_SHORT).show();
                Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone));
                mContext.startActivity(i);


            }
        });
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });
        builder.show();
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        view = View.inflate(mContext, R.layout.foot_expert,null);
        ConsultExpert c = mList.get(i);
        TextView tvDes = (TextView) view.findViewById(R.id.tv_expert_description);
        tvDes.setText(c.getDescription());
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}
